/**
 * $RCSfile: RewardManagerProxy.java,v $
 * $Revision: 1.4 $
 * $Date: 2001/08/20 04:26:27 $
 *
 * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved.
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */

package com.jivesoftware.forum;

import java.util.Iterator;

import com.jivesoftware.forum.model.Authorization;
import com.jivesoftware.forum.model.UnauthorizedException;

/**
 * Protection proxy for RewardManager objects.
 */
public class RewardManagerProxy implements RewardManager{

    protected RewardManager rewardManager;
    protected Authorization authorization;
    protected ForumPermissions permissions;

    public RewardManagerProxy(RewardManager rewardManager,
            Authorization authorization, ForumPermissions permissions)
    {
        this.rewardManager = rewardManager;
        this.authorization = authorization;
        this.permissions = permissions;
    }

    public int getMaxPoints() {
        return rewardManager.getMaxPoints();
    }

    public void setMaxPoints(int numPoints) throws UnauthorizedException
    {
        if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
            rewardManager.setMaxPoints(numPoints);
        }
    }

    public void transferPoints(ForumThread thread, int numPoints)
            throws UnauthorizedException, RewardException
    {
    	String user = thread.getRootMessage().getUser();
        if ( permissions.get(ForumPermissions.SYSTEM_ADMIN) ||
            (user != null && user.equals(authorization.getUserID())) )
        {
            rewardManager.transferPoints(thread, numPoints);
        }
    }

    public void rewardPoints(ForumMessage message, int numPoints)
            throws UnauthorizedException, RewardException
    {
    	String user = message.getForumThread().getRootMessage().getUser();
        if ( permissions.get(ForumPermissions.SYSTEM_ADMIN) ||
            (user != null && user.equals(authorization.getUserID())) )
        {
            rewardManager.rewardPoints(message, numPoints);
        }
    }

    public int getPoints(ForumThread thread) {
        return rewardManager.getPoints(thread);
    }

    public int getPoints(ForumMessage message) {
        return rewardManager.getPoints(message);
    }

    public int getCurrentPoints(String user) {
        return rewardManager.getCurrentPoints(user);
    }

    public Iterator userRewardThreads(String user) {
        return new IteratorProxy(JiveGlobals.THREAD,
                rewardManager.userRewardThreads(user), authorization, permissions);
    }

    public int getTotalPointsRewarded(String user) {
        return rewardManager.getTotalPointsRewarded(user);
    }

    public int getTotalPointsEarned(String user) {
        return rewardManager.getTotalPointsEarned(user);
    }

    public void addPoints(String user, int numPoints)
            throws UnauthorizedException, RewardException
    {
        if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
            rewardManager.addPoints(user, numPoints);
        }
    }
}